package com.android.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.android.R;
import com.android.bean.Good;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.android.bean.postBean.UserPurchase;
import com.android.databinding.ActivityConfirmPurchaseBinding;
import com.android.databinding.ActivityMainBinding;
import com.android.model.purchase.GoodsTradeListener;
import com.android.model.purchase.GoodsTradingModel;
import com.android.model.purchase.GoodsTradingModelimpl;
import com.android.utils.ToastUtil;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class ConfirmPurchaseActivity extends AppCompatActivity implements View.OnClickListener, GoodsTradeListener {
    private Good goods;
    private User user;
    private GoodsTradingModel tradingModel;
    private UserPurchase userPurchase;

    //视图绑定
    private ActivityConfirmPurchaseBinding binding;
    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册eventbus
        EventBus.getDefault().register(this);

//        setContentView(R.layout.activity_confirm_purchase);
        //初始化binding
        binding = ActivityConfirmPurchaseBinding.inflate(getLayoutInflater());
        view = binding.getRoot();
        setContentView(view);

        //初始化View
        initView();
        tradingModel = new GoodsTradingModelimpl();
        //设置购买按钮监听器
        binding.btnConfirm.setOnClickListener(this);

    }

    private void initView() {
        if (goods != null){
            binding.tvPrice.setText(goods.getPrice().toString());
            binding.tvTotalPrice.setText(goods.getPrice().toString());
            binding.tvDesc.setText(goods.getContent());
            binding.tvAddress.setText(goods.getAddr());
            if (goods.getImageUrlList().size()>0){
                Glide.with(this)
                        .load(goods.getImageUrlList().get(0))
                        .into(binding.ivPic);
            }

        }
    }
    //拿取点进来的商品数据
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getGoods(Good data){
        if(data != null){
            this.goods = data;
            Log.e("Goods",goods.getImageUrlList().get(0).toString());
        }else {
            Log.e("Goods","获取失败");

        }
    }
    //拿取点进来的用户数据
    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void getLoginUser(ResponseData<User> responseData){
        this.user = responseData.getData();
    }

    @Override
    protected void onDestroy() {

        //解注eventbus
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_confirm:
                onPurchase();
                Log.e("1",goods.getId().toString());

                break;
        }
    }

    private void onPurchase() {
        userPurchase = new UserPurchase();
        userPurchase.setGoodsId(goods.getId());
        userPurchase.setPrice(goods.getPrice());
        userPurchase.setBuyerId(user.getId());
        userPurchase.setSellerId(goods.gettUserId());
        tradingModel.goodsTrading(this,userPurchase);
    }

    @Override
    public void onSuccessGoodsTrading(ResponseData<String> responseData) {
        Log.e("ConfirmPurchaseActivity",String.valueOf(responseData.getCode()));
        if(responseData.getCode() == 200){
            ToastUtil.showMsg(this,"购买商品成功");
            Intent intent = new Intent(this, HomePageActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onFail(ResponseData<String> responseData) {

    }
}